home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / jnetlib / udpconnection.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  7KB  |  167 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. /*
  30. ** JNetLib
  31. ** Copyright (C) 2000-2001 Nullsoft, Inc.
  32. ** Author: Justin Frankel
  33. ** File: udpconnection.h - JNL UDP connection interface
  34. ** License: see jnetlib.h
  35. **
  36. ** Usage:
  37. **   1. Create a JNL_Connection object, optionally specifying a JNL_AsyncDNS
  38. **      object to use (or NULL for none, or JNL_CONNECTION_AUTODNS for auto),
  39. **      and the send and receive buffer sizes.
  40. **   2. Call connect() to have it connect to a host/port (the hostname will be 
  41. **      resolved if possible).
  42. **   3. call run() with the maximum send/recv amounts, and optionally parameters
  43. **      so you can tell how much has been send/received. You want to do this a lot, while:
  44. **   4. check get_state() to check the state of the connection. The states are:
  45. **        JNL_Connection::STATE_ERROR
  46. **          - an error has occured on the connection. the connection has closed,
  47. **            and you can no longer write to the socket (there still might be 
  48. **            data in the receive buffer - use recv_bytes_available()). 
  49. **        JNL_Connection::STATE_NOCONNECTION
  50. **          - no connection has been made yet. call connect() already! :)
  51. **        JNL_Connection::STATE_RESOLVING
  52. **          - the connection is still waiting for a JNL_AsycnDNS to resolve the
  53. **            host. 
  54. **        JNL_Connection::STATE_CONNECTING
  55. **          - the asynchronous call to connect() is still running.
  56. **        JNL_Connection::STATE_CONNECTED
  57. **          - the connection has connected, all is well.
  58. **        JNL_Connection::STATE_CLOSING
  59. **          - the connection is closing. This happens after a call to close,
  60. **            without the quick parameter set. This means that the connection
  61. **            will close once the data in the send buffer is sent (data could
  62. **            still be being received when it would be closed). After it is 
  63. **            closed, the state will transition to:
  64. **        JNL_Connection::STATE_CLOSED
  65. **          - the connection has closed, generally without error. There still
  66. **            might be data in the receieve buffer, use recv_bytes_available().
  67. **   5. Use send() and send_string() to send data. You can use 
  68. **      send_bytes_in_queue() to see how much has yet to go out, or 
  69. **      send_bytes_available() to see how much you can write. If you use send()
  70. **      or send_string() and not enough room is available, both functions will 
  71. **      return error ( < 0)
  72. **   6. Use recv() and recv_line() to get data. If you want to see how much data 
  73. **      there is, use recv_bytes_available() and recv_lines_available(). If you 
  74. **      call recv() and not enough data is available, recv() will return how much
  75. **      data was actually read. See comments at the function defs.
  76. **
  77. **   7. To close, call close(1) for a quick close, or close() for a close that will
  78. **      make the socket close after sending all the data sent. 
  79. **  
  80. **   8. delete ye' ol' object.
  81. */
  82.  
  83. #ifndef _UDPCONNECTION_H_
  84. #define _UDPCONNECTION_H_
  85.  
  86. #include "asyncdns.h"
  87.  
  88. #define JNL_CONNECTION_AUTODNS ((JNL_AsyncDNS*)-1)
  89.  
  90. class JNL_UDPConnection
  91. {
  92.   public:
  93.     typedef enum 
  94.     { 
  95.       STATE_ERROR, 
  96.       STATE_NOCONNECTION,
  97.       STATE_RESOLVING, 
  98.       STATE_CONNECTED, 
  99.       STATE_CLOSING, 
  100.       STATE_CLOSED 
  101.     } state;
  102.  
  103.     JNL_UDPConnection(short incoming_port=0, JNL_AsyncDNS *dns=JNL_CONNECTION_AUTODNS, int sendbufsize=8192, int recvbufsize=8192);
  104.     ~JNL_UDPConnection();
  105.  
  106.     void setpeer(char *hostname, int port);
  107.     void setpeer(struct sockaddr *addr);
  108.  
  109.     void run(int max_send_bytes=-1, int max_recv_bytes=-1, int *bytes_sent=NULL, int *bytes_rcvd=NULL);
  110.     int  get_state() { return m_state; }
  111.     char *get_errstr() { return m_errorstr; }
  112.  
  113.     void close(int quick=0);
  114.     void flush_send(void) { m_send_len=m_send_pos=0; }
  115.  
  116.     int send_bytes_in_queue(void);
  117.     int send_bytes_available(void);
  118.     int send(char *data, int length); // returns -1 if not enough room
  119.     int send_string(char *line);      // returns -1 if not enough room
  120.  
  121.  
  122.     int recv_bytes_available(void);
  123.     int recv_bytes(char *data, int maxlength); // returns actual bytes read
  124.     unsigned int recv_int(void);
  125.     int recv_lines_available(void);
  126.     int recv_line(char *line, int maxlength); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  127.                                               // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  128.                                               // it will return 1. or if there is no \r or \n and that's all the data
  129.                                               // the connection has.)
  130.     int peek_bytes(char *data, int maxlength); // returns bytes peeked
  131.  
  132.     unsigned long get_interface(void);        // this returns the interface the connection is on
  133.     unsigned long get_remote(void) { return m_saddr.sin_addr.s_addr; } // remote host ip.
  134.     short get_remote_port(void) { return m_remote_port; } // this returns the remote port of connection
  135.  
  136.     void get_last_recv_msg_addr(struct sockaddr *addr) { memcpy(addr, (const void *)&m_laddr, sizeof(struct sockaddr_in)); }
  137.   
  138.   protected:
  139.     int  m_socket;
  140.     short m_remote_port;
  141.     char *m_recv_buffer;
  142.     char *m_send_buffer;
  143.     int m_recv_buffer_len;
  144.     int m_send_buffer_len;
  145.  
  146.     int  m_recv_pos;
  147.     int  m_recv_len;
  148.     int  m_send_pos;
  149.     int  m_send_len;
  150.  
  151.     struct sockaddr_in m_saddr;
  152.     struct sockaddr_in m_iaddr;
  153.     struct sockaddr_in m_laddr;
  154.     char m_host[256];
  155.  
  156.     JNL_AsyncDNS *m_dns;
  157.     int m_dns_owned;
  158.  
  159.     state m_state;
  160.     char *m_errorstr;
  161.  
  162.     int getbfromrecv(int pos, int remove); // used by recv_line*
  163.  
  164. };
  165.  
  166. #endif // _UDPConnection_H_
  167.